package JZOffer;


import sun.nio.ch.SelectorImpl;

// https://leetcode.cn/problems/er-wei-shu-zu-zhong-de-cha-zhao-lcof/?envType=study-plan&id=lcof&plan=lcof&plan_progress=xxj1ozje
// 二维数组中的查找
public class Day005_1 {
    public boolean findNumberIn2DArray(int[][] matrix, int target) {
        for (int[] ints : matrix) {
            int res = search(ints, target);
            if (res >= 0) {
                return true;
            }
        }
        return false;
    }

    private int search(int[] ints, int target) {
        int left = 0;
        int right = ints.length-1;

        while (left <= right) {
            int mid = left + (right - left)/2;
            if (ints[mid] == target) {
                return mid;
            } else if (ints[mid] > target) {
                right = mid - 1;
            } else {
                left = mid + 1;
            }
        }
        return -1;
    }
}
